package mr.ali.videoapp.portal.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import mr.ali.videoapp.portal.Exception.ServiceException;
import mr.ali.videoapp.portal.mapper.ActorMapper;
import mr.ali.videoapp.portal.model.*;
import mr.ali.videoapp.portal.service.IActorService;
import mr.ali.videoapp.portal.service.ICollectionActorService;
import mr.ali.videoapp.portal.service.ICollectionService;
import mr.ali.videoapp.portal.service.ICollectionYurtService;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author Mr-ali
 * @since 2024-08-01
 */
@Service
@Slf4j
public class ActorServiceImpl extends ServiceImpl<ActorMapper, Actor> implements IActorService {

    @Resource
    ActorMapper actorMapper;
    /**
     * 缓存演员表
     */
    List<Actor> actors = new CopyOnWriteArrayList<>();
    @Resource
    private ICollectionActorService collectionActorService;
    @Resource
    private ICollectionService collectionService;

    @Resource
    private ICollectionYurtService collectionYurtService;

    /**
     * collection演员列表
     */
    @Override
    public PageInfo<Actor> getActorList(Integer pageNum, Integer pageSize, Integer sort, Integer gender) {
        LambdaQueryWrapper<Actor> queryWrapper = new LambdaQueryWrapper<>();
        if (sort != null && sort != 0) {
            queryWrapper.eq(Actor::getSort, sort);
        }
        if (gender != null && gender != 0) {
            queryWrapper.eq(Actor::getGender, gender);
        }
        queryWrapper.last("ORDER BY RAND()");
        PageHelper.startPage(pageNum, pageSize);
        List<Actor> actorList = actorMapper.selectList(queryWrapper);
        return new PageInfo<>(actorList);
    }

    /**
     * collection演员信息
     */
    @Override
    public Actor getActorInfo(Integer id) {
        Actor actor = actorMapper.selectById(id);

        CollectionYurt yurts = collectionYurtService.findBySortID(actor.getSort());
        if (yurts != null) {
            actor.setYurts(yurts.getName());
        }
        List<CollectionActor> list = collectionActorService.getCollectionActorByUserId(id);

        // 🟡 准备一个总列表来收集所有的 Collection 数据
        List<Collection> allCollections = new ArrayList<>();

        for (CollectionActor tags : list) {
            List<Collection> videos = collectionService.videoTagsById(tags.getVideoId());
            // 将当前视频的标签添加到总集合中
            if (videos != null && !videos.isEmpty()) {
                allCollections.addAll(videos);
            }
            // 每个 CollectionActor 也可以单独设置自己的 collections（可选）
            tags.setCollection(videos);
        }
        // ✅ 把所有视频的 collections 设置给 actor
        actor.setCollection(allCollections);

        return actor;
    }

    /**
     * collection电影相关的所有演员
     */
    @Override
    public List<Actor> getActorByVideoId(Integer id) {
        QueryWrapper<Actor> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", id);
        List<Actor> tags = actorMapper.selectList(queryWrapper);
        return tags;
    }


}
